﻿using System;
using System.Reflection;
using log4net;

namespace CLFramework.Utility
{
    public class Log
    {
        public static log4net.ILog Instance = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static event EventHandler<LogEventArgs> OnLogged;

        public static void Info(string message)
        {
            log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Info(message);
            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(message, LogLevel.Info));
        }

        public static void Info(string msgFormat, params object[] args)
        {
            string msg = string.Format(msgFormat, args);
            Info(msg);
            
            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(msg, LogLevel.Info));
        }

        public static void Warning(string message)
        {
            log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Warn(message);
            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(message, LogLevel.Warn));
        }

        public static void Warning(string msgFormat, params object[] args)
        {
            string msg = string.Format(msgFormat, args);
            Warning(msg);

            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(msg, LogLevel.Warn));
        }

        public static void Error(string message)
        {
            log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error(message);

            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(message, LogLevel.Error));
        }

        public static void Error(string msgFormat, params object[] args)
        {
            string msg = string.Format(msgFormat, args);
            Error(msg);
        }

        public static void Exception(string message, Exception exception)
        {
            log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error(message, exception);

            if (OnLogged != null)
                OnLogged(null, new LogEventArgs(message, LogLevel.Warn, exception));
        }

        public static void Exception(Exception exception, string msgFormat, params object[] args)
        {
            string msg = string.Format(msgFormat, args);
            Exception(msg, exception);
        }

        /// <summary>
        /// Log Exception
        /// </summary>
        /// <remarks>overload by Lance 2009-8-19</remarks>
        /// <param name="exception">exception to log.</param>
        public static void Exception(Exception exception)
        {
            log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error(exception.Message, exception);
        }
    }

    public class LogEventArgs:EventArgs
    {
        public LogEventArgs(string message, LogLevel level)
        {
            Message = message;
            Level = level;
        }

        public LogEventArgs(string message, LogLevel level, Exception ex)
        {
            Message = message;
            Level = level;
            Exception = ex;
        }

        public string Message{get;set;}
        public LogLevel Level { get; set; }
        public Exception Exception { get; set; }
    }

    public enum LogLevel:uint
	{
        Debug,
        Info,
        Warn,
        Error,
        Fatal
	}
}
